﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using Model;
using BLL;

namespace Web.admin.InnerManage.AwardManage
{
    public partial class AddScore : System.Web.UI.Page
    {
        BLL.ParticipantService participantservice = new BLL.ParticipantService();
        static bool CheckGroup;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["CON_ID"]))
                {
                    if (!participantservice.chekIsGroup(Request.QueryString["CON_ID"]))
                    {
                        CheckGroup = false;
                    }
                    else
                    {
                        CheckGroup = true;
                    }
                    Rp_listBind(Request.QueryString["CON_ID"]);
                }
                else
                {
                    Response.Write("<script>alert('不允许修改地址');window.location.href='InnerPro.aspx?id=" + Request.QueryString["CON_ID"] + "'</script>");
                }
            }
        }

        /// <summary>
        /// 根据竞赛id来绑定相应的repeater
        /// </summary>
        /// <param name="ConId"></param>
        protected void Rp_listBind(string ConId)
        {

            if (!CheckGroup)
            {
                RpList.DataSource = participantservice.GetContestParticipants(ConId);
                RpList.DataBind();
                RpListGroup.Visible = false;
                RpList.Visible = true;
            }
            else
            {
                RpListGroup.DataSource = participantservice.GetV_ParticipantByConId(ConId);
                RpListGroup.DataBind();
                RpListGroup.Visible = true;
                RpList.Visible = false;
            }
        }


        /// <summary>
        /// 录入按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void EnterClick(object sender ,EventArgs e)
        {
            if (!CheckGroup)
            {
                for (int i = 0; i < RpList.Items.Count; i++)
                {
                    TextBox tb = (TextBox)RpList.Items[i].FindControl("Tb_Score");
                    string ParId = ((HiddenField)RpList.Items[i].FindControl("Hf_id")).Value;
                    BoParticipants model = participantservice.getParticipant(ParId);
                    model.PAR_SCORE = Convert.ToInt32(tb.Text);
                    participantservice.UpdateParticipant(model);
                }
            }
            else
            {
                for (int i = 0; i < RpListGroup.Items.Count; i++)
                {
                    TextBox tb = (TextBox)RpListGroup.Items[i].FindControl("Tb_Score");
                    string GroId = ((HiddenField)RpListGroup.Items[i].FindControl("Hf_id")).Value;
                    int score = 0;
                    if (int.TryParse(tb.Text, out score))
                        participantservice.updateGroupScore(Request.QueryString["CON_ID"], score, GroId);
                    else
                        Response.Write("<script>alert('请确保输入的成绩的格式正确')</script>");
                }
            }
            if (cb_update.Checked)
            {
                BLL.PercentService percentservice = new BLL.PercentService();
                string constr = "PER_CONTEST ='" + Request.QueryString["CON_ID"] + "'";
                IList<BoPercent> PercentList = percentservice.Getv_Participant(constr);
                if (PercentList!=null&& PercentList.Count > 0)
                {
                    if (participantservice.innerAward(Request.QueryString["CON_ID"],CheckGroup))
                    {
                        Response.Write("<script>alert('录入成绩成功,并且学生获奖信息保存成功');window.location.href='../InnerAwards.aspx?id=" + Request.QueryString["CON_ID"] + "';</script>");
                        return;
                    }
                    else
                    {
                        Response.Write("<script>alert('录入成绩成功，但学生获奖信息保存失败');window.location.href='../InnerAwards.aspx?id=" + Request.QueryString["CON_ID"] + "';</script>");
                        return;
                    }
                }
                else
                {
                    Response.Write("<script>alert('录入成绩成绩成功，但生成学生获奖等级失败\\n原因：未检测到所设置的百分比，请确保已经设置百分比');window.location.href='../InnerAwards.aspx?id=" + Request.QueryString["CON_ID"] + "';</script>");
                    return;
                }
            }
            else
            {
                Response.Write("<script>alert('操作成功');window.location.href='../InnerAwards.aspx?id=" + Request.QueryString["CON_ID"] + "';</script>");
                return;
            }
        }
    }
}